﻿Public Class td : Inherits element

    Public Column As column
    Public colspan As Integer = 1
    Public rowspan As Integer = 1

    Public Overrides Sub ResizeWidth(ByVal w As Integer)
        For Each Item In Contents
            Item.ResizeWidth(SpanWidth - 2 * Column.table.CellPadding)
        Next
    End Sub

    ReadOnly Property CellPadding As Integer
        Get
            Return CType(Parent.Parent, table).CellPadding
        End Get
    End Property

    ReadOnly Property SpanWidth As Integer
        Get
            Dim w = Column.width
            Dim sc = Column.table.columns.IndexOf(Column)
            For i = 2 To colspan
                w += Column.table.columns(sc + i - 1).width + Column.table.CellSpacing
            Next
            Return w
        End Get
    End Property

    Public Overrides Sub Draw(ByVal g As System.Drawing.Graphics, ByVal P As System.Drawing.Point)
        Dim bgcolor = finalbgcolor
        Dim R As New Rectangle(P, New Size(SpanWidth, Parent.Height))
        If bgcolor <> "" Then
            g.FillRectangle(New SolidBrush(ColorTranslator.FromHtml("#" & bgcolor)), R)
        End If
        g.DrawRectangle(Pens.Red, R)
        P.Offset(CellPadding, CellPadding)
        For Each Item In Contents
            Item.Draw(g, P)
            P.Offset(Item.Width, 0)
        Next
    End Sub

    Public Overrides Sub SetAttributes(a As System.Collections.Generic.Dictionary(Of String, String))
        MyBase.SetAttributes(a)
        For Each Item In a
            Select Case Item.Key
                Case "colspan" : colspan = Item.Value
                Case "rowspan" : rowspan = Item.Value
            End Select
        Next
    End Sub

    Public Overrides ReadOnly Property Height As Integer
        Get
            Return Contents.Max(Function(x) x.Height) + CellPadding * 2
        End Get
    End Property

    Public Overrides ReadOnly Property Width As Integer
        Get
            Return SpanWidth
        End Get
    End Property

End Class